import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kumi_popup_window/kumi_popup_window.dart';
import 'package:testlzhu/common/routers/app_routers.dart';
import 'package:testlzhu/page/equipment/askrent/askrent_view.dart';
import 'package:testlzhu/page/equipment/ceilingheaderdelegate.dart';
import 'package:testlzhu/page/equipment/ceilingheaderdelegate2.dart';
import 'package:testlzhu/page/equipment/rent_page/rent_page_view.dart';
import 'package:testlzhu/view/click_view.dart';
import 'package:testlzhu/view/icon_text.dart';

import 'equipment_logic.dart';

class EquipmentPage extends StatefulWidget {
  const EquipmentPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _EquipmentPageState();
}

class _EquipmentPageState extends State<EquipmentPage> with SingleTickerProviderStateMixin {
  late TabController _tabcontrol;

  late PageController _pageControl;

  final List<String> _tabTitles = ["出租", "求租"];

  late ScrollController _controller;

  late EquipmentLogic logic;

  @override
  void initState() {
    super.initState();
    logic = Get.find<EquipmentLogic>();
    _tabcontrol = TabController(length: 2, vsync: this);
    _pageControl = PageController(initialPage: 0, keepPage: true);
    _controller = ScrollController();
  }

  @override
  Widget build(BuildContext context) {
    var aaa = "false";
    return DefaultTabController(
      length: _tabTitles.length,
      child: Scaffold(
        ///搜索
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(MediaQuery.of(context).size.height * 0.07),
          child: SafeArea(
            top: true,
            child: Container(
              color: Colors.white,
              child: IntrinsicHeight(
                child: Row(
                  children: [
                    IconButton(
                        onPressed: () {
                          Get.back();
                        },
                        icon: const Icon(Icons.arrow_back_ios)),
                    const Text("机械设备"),
                    Expanded(
                      child: Container(
                        alignment: Alignment.centerLeft,
                        margin: const EdgeInsets.only(left: 14, top: 5, bottom: 5, right: 14),
                        height: double.infinity - 10,
                        child: Padding(
                          padding: const EdgeInsets.only(left: 18.0),
                          child: ClickView(
                              onPressed: () {
                                Get.toNamed(AppRoutes.equipmentsearch);
                              },
                              child: const Text(
                                '输入搜索内容',
                                style: TextStyle(color: Color(0xff999999)),
                              )),
                          // child: TextField(
                          //   textInputAction: TextInputAction.search,
                          //   decoration: const InputDecoration(border: InputBorder.none, hintText: "输入搜索内容"),
                          //   onSubmitted: (str) {
                          //     var index = _tabcontrol.index;
                          //     if(index==0){
                          //       RentPageLogic login = Get.find<RentPageLogic>();
                          //       login.getData(str: str);
                          //     }
                          //
                          //   },
                          // ),
                        ),
                        decoration: const BoxDecoration(color: Color(0xfff6f6f6), borderRadius: BorderRadius.all(Radius.circular(5))),
                      ),
                      flex: 1,
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),

        body: CustomScrollView(
          controller: _controller,
          slivers: <Widget>[
            //tabbar
            SliverPersistentHeader(
              delegate: CeilingHeaderDelegate(
                  child: TabBar(
                      controller: _tabcontrol,
                      // indicator: BoxDecoration(borderRadius: BorderRadius.circular(2), color: Colors.amber),
                      indicatorSize: TabBarIndicatorSize.label,
                      labelColor: const Color(0xff333333),
                      unselectedLabelColor: const Color(0xff666666),
                      onTap: (index) {
                        _pageControl.jumpToPage(index);
                      },
                      tabs: _tabTitles.map((e) => Tab(text: e)).toList())),
              pinned: false,
              floating: true,
            ),
            SliverPersistentHeader(
              delegate: CeilingHeaderDelegate2(
                  child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  IconText(
                    '排序',
                    icon: const Icon(Icons.arrow_drop_down),
                    onPressed: () {
                      showpopu1(context, aaa, _controller);
                    },
                  ),
                  IconText(
                    '机型',
                    icon: const Icon(Icons.arrow_drop_down),
                    onPressed: () {
                      logic.settext('sdasdas');
                      setState(() {});
                    },
                  ),
                  const IconText(
                    '品牌',
                    icon: Icon(Icons.arrow_drop_down),
                  ),
                  const IconText(
                    '筛选',
                    icon: Icon(Icons.arrow_drop_down),
                  ),
                ],
              )),
              pinned: true,
              floating: false,
            ),
            SliverFillViewport(
              delegate: SliverChildBuilderDelegate(
                  (_, index) => PageView(
                        controller: _pageControl,
                        onPageChanged: (index) {
                          // _tabcontrol.animateTo(index);
                          _tabcontrol.index = index;
                        },
                        children: [
                          RentPagePage(),
                          AskrentPage(),
                        ],
                      ),
                  childCount: 1),
            ),
          ],
        ),
      ),
    );
  }

  void showpopu1(BuildContext context, String aaa, ScrollController controller) {
    LogUtil.e(controller.offset);

    showPopupWindow(context, gravity: KumiPopupGravity.centerTop, childFun: (pop) {
      return StatefulBuilder(
        key: GlobalKey(),
        builder: (context, setState) {
          return Container(
            height: 200,
            // width: 300,
            color: Colors.white,
            child: MediaQuery.removePadding(
              context: context,
              removeTop: true,
              child: ListView.custom(
                childrenDelegate: SliverChildBuilderDelegate((context, index) {
                  return ListTile(
                    title: Text('text2===$index'),
                  );
                }, childCount: 10),
              ),
//asdasdsadasdasdsadasdsadasdsd
              /*    child: ListView.builder(
                  itemCount: 10,
                  itemBuilder: (context,index){
                return ListTile(
                  title: Text('text===$index'),
                );
              }),*/
            ),
          );
        },
      );
    }, offsetY: getheight(controller, context), bgColor: Colors.transparent);
  }

  //获取弹出位置
  double getheight(ScrollController controller, BuildContext context) {
    double aa = 0;
    double height = Size.fromHeight(MediaQuery.of(context).size.height * 0.07).height;
    aa += height;
    var offset = controller.offset;

    if (offset > 50) {
      aa += 70;
    } else {
      aa += 120;
    }
    return aa;
  }
}
